package cn.iocoder.yudao.module.system.controller.admin.workstore;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workstore.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workstore.WorkStoreDO;
import cn.iocoder.yudao.module.system.service.workstore.WorkStoreService;

@Tag(name = "管理后台 - 工单入库")
@RestController
@RequestMapping("/system/work-store")
@Validated
public class WorkStoreController {

    @Resource
    private WorkStoreService workStoreService;

    @PostMapping("/create")
    @Operation(summary = "创建工单入库")
    @PreAuthorize("@ss.hasPermission('system:work-store:create')")
    public CommonResult<Integer> createWorkStore(@Valid @RequestBody List<WorkStoreSaveReqVO> createReqVO) {
        return success(workStoreService.createWorkStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单入库")
    @PreAuthorize("@ss.hasPermission('system:work-store:update')")
    public CommonResult<Boolean> updateWorkStore(@Valid @RequestBody WorkStoreSaveReqVO updateReqVO) {
        workStoreService.updateWorkStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-store:delete')")
    public CommonResult<Boolean> deleteWorkStore(@RequestParam("id") Integer id) {
        workStoreService.deleteWorkStore(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除工单入库")
                @PreAuthorize("@ss.hasPermission('system:work-store:delete')")
    public CommonResult<Boolean> deleteWorkStoreList(@RequestParam("ids") List<Integer> ids) {
        workStoreService.deleteWorkStoreListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-store:query')")
    public CommonResult<WorkStoreRespVO> getWorkStore(@RequestParam("id") Integer id) {
        WorkStoreRespVO workStore = workStoreService.getWorkStore(id);
        return success(workStore);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单入库分页")
    @PreAuthorize("@ss.hasPermission('system:work-store:query')")
    public CommonResult<PageResult<WorkStoreRespVO>> getWorkStorePage(@Valid WorkStorePageReqVO pageReqVO) {
        PageResult<WorkStoreRespVO> pageResult = workStoreService.getWorkStorePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单入库 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-store:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkStoreExcel(@Valid WorkStorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkStoreRespVO> list = workStoreService.getWorkStorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单入库.xls", "数据", WorkStoreRespVO.class,
                        BeanUtils.toBean(list, WorkStoreRespVO.class));
    }

}
